package com.keyinong.jiashiyuan.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.keyinong.jiashiyuan.R;
import com.keyinong.jiashiyuan.tools.Api;

public class BaiduActivity extends Activity {
	// 定位相关
		LocationClient mLocClient;
		public MyLocationListenner myListener = new MyLocationListenner();
		private LocationMode mCurrentMode;
		BitmapDescriptor mCurrentMarker;

		MapView mMapView;
		BaiduMap mBaiduMap;

		// UI相关
		OnCheckedChangeListener radioButtonListener;
		Button bt_daohang;
		boolean isFirstLoc = true;// 是否首次定位

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_baidu);
			bt_daohang = (Button) findViewById(R.id.bt_daohang);
			mCurrentMode = LocationMode.NORMAL;
			bt_daohang.setOnClickListener(new OnClickListener() {
				SharedPreferences sp = getSharedPreferences("spReg",Context.MODE_PRIVATE);
					String Latitude = sp.getString("Latitude", "");
					String Longitude = sp.getString("Longitude", "");

					 String origin= Latitude+","+Longitude;
					 String origin_region;
					 String destination_region;
					 String coord_type;
					 String zoom;
					 String src;
					 String destination=getIntent().getStringExtra("lat")+","+getIntent().getStringExtra("lng");
					 String mode="driving";
					 String region;
				@Override
				public void onClick(View v) {
					
					 Api api =new Api();
					api.goToNaviActivity(getApplicationContext(), origin, destination, mode, region, origin_region, destination_region, coord_type, zoom, src);
				}
			});
//			OnClickListener btnClickListener = new OnClickListener() {
//				public void onClick(View v) {
//					switch (mCurrentMode) {
//					case NORMAL:
//						requestLocButton.setText("跟随");
//						mCurrentMode = LocationMode.FOLLOWING;
//						mBaiduMap
//								.setMyLocationConfigeration(new MyLocationConfiguration(
//										mCurrentMode, true, mCurrentMarker));
//						break;
//					case COMPASS:
//						requestLocButton.setText("普通");
//						mCurrentMode = LocationMode.NORMAL;
//						mBaiduMap
//								.setMyLocationConfigeration(new MyLocationConfiguration(
//										mCurrentMode, true, mCurrentMarker));
//						break;
//					case FOLLOWING:
//						requestLocButton.setText("罗盘");
//						mCurrentMode = LocationMode.COMPASS;
//						mBaiduMap
//								.setMyLocationConfigeration(new MyLocationConfiguration(
//										mCurrentMode, true, mCurrentMarker));
//						break;
//					}
//				}
//			};
			//requestLocButton.setOnClickListener(btnClickListener);

//			RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup);
//			radioButtonListener = new OnCheckedChangeListener() {
//				@Override
//				public void onCheckedChanged(RadioGroup group, int checkedId) {
//					if (checkedId == R.id.defaulticon) {
//						// 传入null则，恢复默认图标
//						mCurrentMarker = null;
//						mBaiduMap
//								.setMyLocationConfigeration(new MyLocationConfiguration(
//										mCurrentMode, true, null));
//					}
//					if (checkedId == R.id.customicon) {
//						// 修改为自定义marker
//						mCurrentMarker = BitmapDescriptorFactory
//								.fromResource(R.drawable.icon_geo);
//						mBaiduMap
//								.setMyLocationConfigeration(new MyLocationConfiguration(
//										mCurrentMode, true, mCurrentMarker));
//					}
//				}
//			};
//			group.setOnCheckedChangeListener(radioButtonListener);

			// 地图初始化
			mMapView = (MapView) findViewById(R.id.bmapView);
			mBaiduMap = mMapView.getMap();
			// 开启定位图层
			mBaiduMap.setMyLocationEnabled(true);
			// 定位初始化
			mLocClient = new LocationClient(this);
			mLocClient.registerLocationListener(myListener);
			LocationClientOption option = new LocationClientOption();
			option.setOpenGps(true);// 打开gps
			option.setCoorType("bd09ll"); // 设置坐标类型
			option.setScanSpan(1000);
			mLocClient.setLocOption(option);
			mLocClient.start();
		}

		/**
		 * 定位SDK监听函数
		 */
		public class MyLocationListenner implements BDLocationListener {

			@Override
			public void onReceiveLocation(BDLocation location) {
				// map view 销毁后不在处理新接收的位置
				if (location == null || mMapView == null)
					return;
				MyLocationData locData = new MyLocationData.Builder()
						.accuracy(location.getRadius())
						// 此处设置开发者获取到的方向信息，顺时针0-360
						.direction(100).latitude(location.getLatitude())
						.longitude(location.getLongitude()).build();
				String Latitude =""+location.getLatitude();
				String Longitude =""+location.getLongitude();
				SharedPreferences sp = getSharedPreferences("spReg",Context.MODE_PRIVATE);
				Editor editor = sp.edit();
				editor.putString("Latitude", Latitude);
				editor.putString("Longitude", Longitude);
				editor.commit();
//				Log.e("zq", ""+location.getLatitude());
//				Log.e("zq", ""+location.getLongitude());
				mBaiduMap.setMyLocationData(locData);
				if (isFirstLoc) {
					isFirstLoc = false;
					LatLng ll = new LatLng(location.getLatitude(),
							location.getLongitude());
					MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
					mBaiduMap.animateMapStatus(u);
				}
			}

			public void onReceivePoi(BDLocation poiLocation) {
			}
		}

		@Override
		protected void onPause() {
			mMapView.onPause();
			super.onPause();
		}

		@Override
		protected void onResume() {
			mMapView.onResume();
			super.onResume();
		}

		@Override
		protected void onDestroy() {
			// 退出时销毁定位
			mLocClient.stop();
			// 关闭定位图层
			mBaiduMap.setMyLocationEnabled(false);
			mMapView.onDestroy();
			mMapView = null;
			super.onDestroy();
		}

}
